package com.example.zhangxin.zyfypt_124zx.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.zhangxin.zyfypt_124zx.R;
import com.example.zhangxin.zyfypt_124zx.adapter.TwareAdapter;
import com.example.zhangxin.zyfypt_124zx.bean.TwareBean;
import com.example.zhangxin.zyfypt_124zx.iface.TwareListener;
import com.example.zhangxin.zyfypt_124zx.model.TwareModel;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;


/**
 * Created by Neusoft on 2017/4/9.
 */

public class Fragment2 extends Fragment {

    private View view=null;
    private Context context;
    private RecyclerView recyclerView;//recyclerView控件
    //private RecyclerView.LayoutManager layoutManager;//..的布局管理
    private TwareAdapter myAdapter;//..的适配器

    private List<TwareBean> list=null;//保存数据
    private int page=1; // 代表页数，并初始化为1，代表第1页。
    private int lastVisibleItemPosition;//最后一条可见条目的位置
    private LinearLayoutManager  layoutManager;//显示布局效果

    private String sessionID="";//用户名密码
    private SharedPreferences sp;

    TwareListener twareListener=new TwareListener() {
        @Override
        public void onResponse(List<TwareBean> beanList) {
           // list=beanList;//拿到网络数据
            if(page==1)
            {
                list=beanList;
            }
            else {
                list.removeAll(beanList);
                list.addAll(beanList);
            }
            myAdapter.setList(list);
            myAdapter.notifyDataSetChanged();//通知更新
        }

        @Override
        public View onFail(String msg) {
            Toast.makeText(getActivity(), "失败："+msg,Toast.LENGTH_SHORT).show();
            return null;

        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
       //动态加载Fragment1的布局文件
        view=inflater.inflate(R.layout.fragment2,container,false);
        //获取布局上的控件，注意通过view获取

        //注意Toast第一个参数上下文
        Toast.makeText(context, "加载Fragment课件", Toast.LENGTH_SHORT).show();
        initRecyclerView();

        sp=context.getSharedPreferences("login",MODE_PRIVATE);
        readSP();
        TwareModel model=new TwareModel();
        model.getResultList("tware",page,sessionID,twareListener);

        //返回动态生成的view
        return view;

    }

    private void initRecyclerView() {
        recyclerView=(RecyclerView)view.findViewById(R.id.recyclerView);
        //创建默认的线性布局
        layoutManager=new LinearLayoutManager(getActivity());
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item高度，提高性能
        recyclerView.setHasFixedSize(true);
        //获取数据
        //list=getData();
        //实例化Adapter
        myAdapter=new TwareAdapter(getActivity());
        myAdapter.setList(list);//传递数据
        //绑定recyclerview和adapter
        recyclerView.setAdapter(myAdapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page += 1;
                    //再次实例化ArticleModel，调用方法获取网络数据，请求新一页数据
                    TwareModel twareModel = new TwareModel();
                    twareModel.getResultList("tware",page,sessionID,twareListener);
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=layoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位置
            }
        });
    }
    private void readSP() {
        sessionID=sp.getString("sessionid",null);

    }
}
